/*****************************************************************************/ /*!
* file  : systask.c
* creat : apleilx
* data  : 2017/11/05
*******************************************************************************/
#define _SYSTASK_MODULE_
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "common.h"
#include "swap.h"
#include "nor_cmd.h"
#include "nand_w25n.h"
#include "bsp_api.h"
#include "usermanage.h"

void system_info(void);
void sys_tick_exe(void);
void sys_led_ctr(void);
void sys_key_ctr(void);

/*****************************************************************************
* @brief   system task.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void task_system(const void *argument)
{
	bsp_Init();

	os_obj_create();

	app_timer_task_add(sys_tick_exe);

	user_manage_init();
	
	sys_init();
	
	while (1)
	{
		sys_st.info.stack_used = stack_used();

		bsp_dwt_check();

		sys_st.u_tick = app_tick_get();

		// 虚拟RTC
		pulse_create(&sys_st.pulse, sys_st.u_tick);
		if (sys_st.pulse.pulse_1s)
		{
			TimeRun(&sys_st.rtc_time);
		}

		// LED
		if (sys_st.pulse.pulse_10ms)
			sys_led_ctr();

		sys_key_ctr();
		
		sys_st.buff = malloc(2*1024);
		
		NOP1();
		osDelay(ms_ticks(10));
		NOP1();
		
		free(sys_st.buff);
	}
}

/*****************************************************************************
*
* @brief   led control.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_led_ctr(void)
{
	static word_tt st = {1};

	if(sys_st.pulse.pulse_20ms)
	{
		o_led1.out(0);
		o_led2.out(0);
		o_led3.out(0);	
	}
	
	// 位移
	if (sys_st.pulse.pulse_200ms)
	{
		st.uval = bit16_roll_shift_left(st.uval, 5, 1);
		// 输出
		o_led1.out(st.bits.b0);
		o_led2.out(st.bits.b1);
		o_led3.out(st.bits.b2);		
	}
}

/*****************************************************************************
*
* @brief   sys_tick_exe
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void sys_tick_exe(void)
{
	static uint8_t ticks = 0;
	ticks ++;
	
	if((ticks & 0x03) == 0)
	{
		uint16_t key_rd = 0;
		
		if(key1.in_st())
			key_rd |= 1;
		if(key2.in_st())
			key_rd |= 2;
		if(key3.in_st())
			key_rd |= 4;
		
		key_st_sample(&sys_st.key, 4, key_rd);
	}
}

/*****************************************************************************
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void system_info(void)
{
	RCC_ClocksType clocks;

	RCC_GetClocksFreqValue(&clocks);
	
	dbg_puts("\n\n");
	dbg_puts("===========================================================\n");
	dbg_puts("nation rt-thread start \n");
	dbg_puts("===========================================================\n");
	dbg_print("HCLK = %d MHZ, PCLK1 = %d MHZ, PCLK2 = %d MHZ\n",
			  clocks.HclkFreq / 1000000,
			  clocks.Pclk1Freq / 1000000,
			  clocks.Pclk2Freq / 1000000);
	dbg_puts("===========================================================\n");
}

/*****************************************************************************
* @brief   system info.
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
#include "swap.h"
void swap_test(void)
{
	uint16_t src[16];
	uint16_t enc[16];
	uint16_t dec[16];
	
	set_u16(src, 0, sizeof(src)/2);
	set_u16(enc, 0, sizeof(src)/2);
	set_u16(dec, 0, sizeof(src)/2);
	src[0] = src[1] =UINT16_MAX;
	swap_cbit_map_u16(src, enc, 5, 0);
	swap_cbit_map_u16(enc, dec, 5, 0);
	dbg_u16_print("B:SRC 1:\n", src, 6, 1);
	dbg_u16_print("B:ENC 1:\n", enc, 6, 1);
	dbg_u16_print("B:DEC 1:\n", dec, 6, 1);
	
	set_u16(src, 0, sizeof(src)/2);
	set_u16(enc, 0, sizeof(src)/2);
	set_u16(dec, 0, sizeof(src)/2);
	src[0] = src[3] =UINT16_MAX;
	swap_cbit_map_u16(src, enc, 5, 0);
	swap_cbit_map_u16(enc, dec, 5, 0);
	dbg_u16_print("B:SRC 2:\n", src, 6, 1);
	dbg_u16_print("B:ENC 2:\n", enc, 6, 1);
	dbg_u16_print("B:DEC 2:\n", dec, 6, 1);
	
	set_u16(src, 0, sizeof(src)/2);
	set_u16(enc, 0, sizeof(src)/2);
	set_u16(dec, 0, sizeof(src)/2);
	src[0] = src[1] =UINT16_MAX;
	swap_cbit_map_u16(src, enc, -5, 20);
	swap_cbit_map_u16(enc, dec, -5, 20);
	dbg_u16_print("B:SRC 3:\n", src, 6, 1);
	dbg_u16_print("B:ENC 3:\n", enc, 6, 1);
	dbg_u16_print("B:DEC 3:\n", dec, 6, 1);
	
	set_u16(src, 0, sizeof(src)/2);
	set_u16(enc, 0, sizeof(src)/2);
	set_u16(dec, 0, sizeof(src)/2);
	src[0] = src[3] =UINT16_MAX;
	swap_cbit_map_u16(src, enc, 50, -1);
	swap_cbit_map_u16(enc, dec, 50, -1);
	dbg_u16_print("B:SRC 4:\n", src, 6, 1);
	dbg_u16_print("B:ENC 4:\n", enc, 6, 1);
	dbg_u16_print("B:DEC 4:\n", dec, 6, 1);
}
obj_init_order5(swap_test);

obj_init_order1(system_info);
obj_init_order2(sys_tick_exe);
obj_app_order(sys_led_ctr);
obj_app_order(sys_key_ctr);
obj_app_order(atask1);
obj_app_order(atask2);
obj_app_order(atask3);
obj_app_order(atask4);
obj_app_order(atask5);


obj_init_order4(ainit41);
obj_init_order4(ainit42);
obj_init_order4(ainit43);
obj_init_order3(ainit31);
obj_init_order3(ainit32);
obj_init_order3(ainit33);
obj_init_order2(ainit21);
obj_init_order2(ainit22);
obj_init_order2(ainit23);
obj_init_order1(ainit11);
obj_init_order1(ainit12);
obj_init_order1(ainit13);

